<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>P218 动手实践：表单验证</title>
</head>

<body>
    <table>
        <tr>
            <th>用户名称：</th>
            <td>
                <input type="text" name="username" placeholder="长度4~12，英文大小写字母">
            </td>
            <td>
                <div></div>
            </td>
        </tr>
        <tr>
            <th>用户密码：</th>
            <td>
                <input type="password" name="pwd" placeholder="长度6~20，大小写字母、数字或下划线">
            </td>
            <td>
                <div></div>
            </td>
        </tr>
        <tr>
            <th>确认密码：</th>
            <td>
                <input type="password" name="repwd" placeholder="请再次输入密码进行确认">
            </td>
            <td>
                <div></div>
            </td>
        </tr>
        <tr>
            <th>手机号码：</th>
            <td>
                <input type="text" name="tel" placeholder="13、14、15、17、18开头的11位手机号">
            </td>
            <td>
                <div></div>
            </td>
        </tr>
        <tr>
            <th>电子邮箱：</th>
            <td>
                <input type="text" name="email" placeholder="用户名@域名（域名后缀至少2个字符）">
            </td>
            <td>
                <div></div>
            </td>
        </tr>
        <tr>
            <td colspan="3"><input type="submit" value="注册"></td>
        </tr>
    </table>
    <script>
        var inputs = document.getElementsByTagName('input');
        for (var i = 0; i < inputs.length - 1; ++i) {
            inputs[i].onblur = inputBlur;
        }
        function inputBlur() {
            var name = this.name;
            var val = this.value;
            var tips = this.placeholder;
            var tips_obj = this.parentNode.nextSibling.firstChild;
            val = val.trim();
            if (!val) {
                error(tips_obj, '输入框不能为空');
                return false;
            }
            var reg_msg = getRegMsg(name, tips);
            if (reg_msg['reg'].test(val)) {
                success(tips_obj, reg_msg['msg']['success']);
            } else {
                error(tips_obj, reg_msg['msg']['error']);
            }
        }
        function success(obj, msg) {
            obj.className = 'success';
            obj.innerHTML = msg;
        }
        function error(obj, msg) {
            obj.className = 'error';
            obj.innerHTML = msg + '，请重新输入';
        }
        function getRegMsg(name, tips) {
            var reg = msg = '';
            switch (name) {
                case 'username':
                    reg = /^[a-zA-z]{4,12}$/;
                    msg = { 'success': '用户名输入正确', 'error': tips };
                    break;
                case 'pwd':
                    reg = /^\w{6,20}$/;
                    msg = { 'success': '密码输入正确', 'error': tips };
                    break;
                case 'repwd':
                    var con = document.getElementsByTagName('input')[1].value;
                    reg = RegExp('^' + con + '$');
                    msg = { 'success': '两次密码输入正确', 'error': '两次输入的密码不一致' };
                    break;
                case 'tel':
                    reg = /^1[34578]\d{9}$/;
                    msg = { 'success': '手机号码输入正确', 'error': tips };
                    break;
                case 'email':
                    reg = /^(\w+(\_|\-|\.)*)+@(\w+(\-)?)+(\.\w{2,})+$/;
                    msg = { 'success': '邮箱输入正确', 'error': tips };
                    break;
            }
            return { 'reg': reg, 'msg': msg };
        }
    </script>
    <a href="../首页/page09.html">
        <h2>返回上一页面</h2>
    </a>
</body>

</html>